<?php

namespace Admin\Model;

use Zend\InputFilter\Factory as InputFactoy;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Admin\Model\BaseModel;

class Product extends BaseModel {

	public $id;
	public $name;
	public $desc;
	public $price;
	public $p_id;
	protected $inputFilter;
	private $_dbAdapter;

	public function exchangeArray($data) {
		$properties = $this->getArrayCopy($this);
		foreach ($properties as $property => $value)
			$this->{$property} = isset($data[$property]) ? $data[$property] : null;
		if(isset($data['category'])) $this->category = $data['category'];
	}
	public function setDbAdapter($dbAdapter) {
        $this->_dbAdapter = $dbAdapter;
    }

    public function getDbAdapter() {
        return $this->_dbAdapter;
   }
	public function getInputFilter() {
		if (!$this->inputFilter) {
			$inputFilter = new InputFilter();
			$factory = new InputFactoy();
			$dbAdapter = $this->getDbAdapter();
			
			$inputFilter->add($factory->createInput(array(
				'name' => 'id',
				'required' => true,
				'filters' => array(
					array('name' => 'Int')
				),
			)));

			$inputFilter->add($factory->createInput(array(
						'name' => 'name',
						'required' => true,
						'filter' => array(
							array('name' => 'StripTags'),
							array('name' => 'StringStrim'),
						),
						'validators' => array(
							array(
								'name' => 'StringLength',
								'options' => array(
									'encoding' => 'UTF-8',
									'min' => 1,
									'max' => 100,
								),
							),
						),
			)));

			$inputFilter->add($factory->createInput(array(
						'name' => 'desc',
						'required' => false,
						'filters' => array(
							array('name' => 'StripTags'),
							array('name' => 'StringTrim'),
						),
						'validators' => array(
							array(
								'name' => 'StringLength',
								'options' => array(
									'encoding' => 'UTF-8',
									'min' => 0,
									'max' => 300,
								),
							),
						),
			)));

			$inputFilter->add($factory->createInput(array(
						'name' => 'price',
						'required' => true,
						'filters' => array(
							array('name' => 'Int'),
						),
			)));

			$inputFilter->add($factory->createInput(array(
						'name' => 'p_id',
						'required' => true,
						'filters' => array(
							array('name' => 'Int'),
						),
						'validators'	=> array(
							array(
								'name'	=> 'Db\RecordExists',
								'options'	=> array(
									'table'	=> 'category',
									'field'	=> 'id',
									'adapter'	=> $dbAdapter,
									
								)
							)
						)
			)));
			$this->inputFilter = $inputFilter;
		}
		return $this->inputFilter;
	}

}